package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.ui.HostActivity;
import net.oschina.app.ui.dialog.PhoneDialog;
import net.oschina.app.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YL on 2017/3/31.
 */
public class ContentFragment extends Fragment {
    private View mView;
    private String texts[] = {"综合", "动弹", "", "发现", "我"};
    private int imageButton[] = {R.drawable.widget_bar_news_selector,
            R.drawable.widget_bar_tweet_selector,
            0,
            R.drawable.widget_bar_explore_selector,
            R.drawable.widget_bar_me_selector};
    private List<Class> fragmentArray = new ArrayList<>();
    private FragmentTabHost mTab_host;
    private ImageView mAdd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_content, container, false);
        mAdd = (ImageView) mView.findViewById(R.id.tab_host_img);
        mTab_host = (FragmentTabHost) mView.findViewById(R.id.tab_host);
        mTab_host.setup(getContext(), getActivity().getSupportFragmentManager(), R.id.content_frame);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initEvent();
    }

    private void initEvent() {
        mTab_host.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {

            }
        });
        mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhoneDialog dialog = new PhoneDialog(getActivity());
                dialog.show();
            }
        });
        mAdd.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                startActivity(new Intent(getActivity(),HostActivity.class));
                return false;
            }
        });
    }

    private void initView() {
        fragmentArray.add(NewsViewPagerFragment.class);
        fragmentArray.add(TweetsViewPagerFragment.class);
        fragmentArray.add(TweetsViewPagerFragment.class);
        fragmentArray.add(ExploreFragment.class);
        fragmentArray.add(MyInformationFragment.class);
        for (int i = 0; i < texts.length; i++) {

            TabHost.TabSpec tabSpec = mTab_host.newTabSpec("news" + i);
            View v=getIndicatorView(texts[i], imageButton[i]);
            tabSpec.setIndicator(v);
           if(i==2){
               v.setVisibility(View.INVISIBLE);
           }
            mTab_host.addTab(tabSpec, fragmentArray.get(i), null);

        }


    }

    private View getIndicatorView(String name, int imgId) {
        View indicatorView = View.inflate(UIUtils.getContext(), R.layout.indicator_item, null);
        ImageView img = (ImageView) indicatorView.findViewById(R.id.indicator_img);
        TextView tv = (TextView) indicatorView.findViewById(R.id.indicator_tv);
        tv.setText(name);
        img.setBackgroundResource(imgId);
        return indicatorView;
    }
}
